<?php
/**
 * @Author: Xu Liu email:2867557054@qq.com
 * @Date:   2016-12-15 13:31:33
 * @Last Modified by:   Xu Liu
 * @Last Modified time: 2018-04-26 17:45:55
 */

if (!defined('IN_DISCUZ')) {
    exit('Access Denied');
}
global $_G;
if (FORMHASH != trim($_GET['formhash'])) {
    echo 'error';
    exit;
}
require_once libfile('function/jameson', 'plugin/jameson_read');
init_jameson();
if(!DB::result_first('SELECT count(*) FROM %t WHERE '.DB::field('skey','xf512baidu_access'),array('common_setting'))){
    $access_token = $access_time = $access_apikey = 0;
    C::t('common_setting')->insert(array('skey'=>'xf512baidu_access','svalue'=>0));
}
if(!DB::result_first('SELECT count(*) FROM %t WHERE '.DB::field('skey','xf512baidu_accesstime'),array('common_setting'))){
    $access_token = $access_time = $access_apikey = 0;
    C::t('common_setting')->insert(array('skey'=>'xf512baidu_accesstime','svalue'=>0));
}
if(!DB::result_first('SELECT count(*) FROM %t WHERE '.DB::field('skey','xf512baidu_apikey'),array('common_setting'))){
    $access_token = $access_time = $access_apikey = 0;
    C::t('common_setting')->insert(array('skey'=>'xf512baidu_apikey','svalue'=>0));
}
$access_token = trim(C::t('common_setting')->fetch('xf512baidu_access'));
$access_apikey = trim(C::t('common_setting')->fetch('xf512baidu_apikey'));
$access_time  = (int) C::t('common_setting')->fetch('xf512baidu_accesstime');
if(($_G['jameson_read']['apikey']!= $access_apikey) || !$access_token || !$access_time || (TIMESTAMP > $access_time)){
    $acc = curlPost('https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id='.$_G['jameson_read']['apikey'].'&client_secret='.$_G['jameson_read']['secretkey']);
    $acc = json_decode($acc,true);
    if($acc['access_token']){
        $access_token = $acc['access_token'];
        C::t('common_setting')->update('xf512baidu_access',$acc['access_token']);
        C::t('common_setting')->update('xf512baidu_apikey',$_G['jameson_read']['apikey']);
        C::t('common_setting')->update('xf512baidu_accesstime',TIMESTAMP+3600*700);
    }
}
if($access_token){
    $tid = intval($_GET['tid']);
    $pid = intval($_GET['pid']);
    $message = C::t("forum_post")->fetch('tid:' . $tid, $pid, true);
    $message = preg_replace(array(
            '/\[attach\](.*?)\[\/attach\]/ism',
            '/\[audio([^\]]*)\](.*?)\[\/audio\]/ism',
            '/\[media([^\]]*)\](.*?)\[\/media\]/ism',
            '/\[img([^\]]*)\](.*?)\[\/img\]/ism',
            "/\[\w+[^\]]\]/i", "/\[\/\w+\]/ism",
            "/[\r|\n|\s]/",
            "/&[#|\d|\w]*;/i",
            "/jameson_read/i"
        ), array("","","","","","","","",), trim($message['message']));
    $message = strip_tags($message);
    $message = strtolower(CHARSET)=='utf-8'?$message:diconv($message,CHARSET,'utf-8');
    $audiosize    = 400;
    $length  = ceil(mb_strlen($message, 'utf-8') / $audiosize);
    $onlyid = md5($_G['siteurl'].$_G['uid'].$_G['clientip'].$_SERVER['REQUEST_TIME_FLOAT'].$_SERVER['REMOTE_PORT'].random(12));
    $data = array();
    for ($i = 0; $i < $length; $i++) {
        $m = trim(mb_substr($message, $i * $audiosize, $audiosize, 'utf-8'));
        $data[] = curlPost('http://tsn.baidu.com/text2audio?lan=zh&ctp=1&cuid='.$onlyid.'&tok='.$access_token.'&tex='.$m);
    }
    ob_clean();
    header("Content-Type:audio/mp3");
    echo implode('',$data);
    exit;
}else{
    echo 'accesserror';
    exit;
}
function curlPost($url){ 
        $ch = curl_init();  
        curl_setopt($ch, CURLOPT_URL, $url);  
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
        $ret = curl_exec($ch);  
        if(preg_match("/err_no/i",$ret)){
            $ret = json_decode($ret,true);
            $ret = 'baiduyuyin'.$ret['err_no'];
            return $ret;
        }
        curl_close($ch);
    return $ret;
}